package eu.codlab.eirb.caslibrary;

import android.accounts.AbstractAccountAuthenticator;
import android.accounts.Account;
import android.accounts.AccountAuthenticatorResponse;
import android.accounts.AccountManager;
import android.accounts.NetworkErrorException;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

public class TwittEirbAuthenticator extends AbstractAccountAuthenticator
{

	public static final String ACCOUNT_TYPE = "eu.codlab.eirb.caslibrary.authenticator";
	private Context mContext;

	public TwittEirbAuthenticator(Context context){
		super(context);
		mContext = context;
	}

	@Override
	public Bundle addAccount(AccountAuthenticatorResponse response, String accountType, String authTokenType, String[] requiredFeatures, Bundle options) throws NetworkErrorException
	{
		final Bundle result;
		final Intent intent;

		AccountManager am = AccountManager.get(mContext);
		Account[] aca = am.getAccountsByType(ACCOUNT_TYPE);
		if(aca.length==0){
			intent = new Intent(this.mContext, TwittEirbCreateAccountActivity.class);
			intent.putExtra(TwittEirbAuthenticator.ACCOUNT_TYPE, authTokenType);
			intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
			result = new Bundle();
			result.putParcelable(AccountManager.KEY_INTENT, intent);
		}else{
			result = new Bundle();
		}
		return result;
	}

	@Override
	public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account, Bundle options) throws NetworkErrorException
	{
		//final String password = options.getString(AccountManager.KEY_PASSWORD);
        final Bundle result = new Bundle();
        result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, true);
        return result;
	}

	@Override
	public Bundle editProperties(AccountAuthenticatorResponse response, String accountType)
	{
		return null;
	}

	@Override
	public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException{
		final Bundle result;
		result = new Bundle();


		AccountManager am = AccountManager.get(mContext);
		String username = account.name;
		String password = am.getPassword(account);


		Intent i = new Intent();
		i.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
		i.putExtra("user", username);
		String [] _options = options != null ? options.getStringArray("services") : null;
		if(_options == null){
			_options = authTokenType.split(";");
		}

		AuthenticatorHelper helper = new AuthenticatorHelper();
		
		String [] retour = helper.infos(username, password, _options);
		if(retour != null){
			i.putExtra("authToken", "aeiouy");
			i.putExtra("services", retour);
		}else{
			//log("error","error while login in");
		}
		result.putParcelable(AccountManager.KEY_INTENT, i);
		return result;
	}


	@Override
	public String getAuthTokenLabel(String authTokenType)
	{
		return null;
	}

	@Override
	public Bundle hasFeatures(AccountAuthenticatorResponse response, Account account, String[] features) throws NetworkErrorException
	{
		return null;
	}

	@Override
	public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException
	{
		return null;
	}


}